package com.qianren.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TimeZone;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Common {
	/**
	 * String -> int
	 * 
	 * @param param
	 * @return
	 */
	public static int parseInt(String param) {
		int i = 0;
		try {
			i = Integer.parseInt(param);
		} catch (Exception e) {
			i = (int) parseFloat(param);
		}
		return i;
	}

	/**
	 * String -> long
	 * 
	 * @param param
	 * @return
	 */
	public static long parseLong(String param) {
		long l = 0;
		try {
			l = Long.parseLong(param);
		} catch (Exception e) {
			l = (long) parseDouble(param);
		}
		return l;
	}

	/**
	 * String -> float
	 * 
	 * @param param
	 * @return
	 */
	public static float parseFloat(String param) {
		float f = 0;
		try {
			f = Float.parseFloat(param);
		} catch (Exception e) {
			//
		}
		return f;
	}

	/**
	 * String -> double
	 * 
	 * @param param
	 * @return
	 */
	public static double parseDouble(String param) {
		double d = 0;
		try {
			d = Double.parseDouble(param);
		} catch (Exception e) {
			//
		}
		return d;
	}

	/**
	 * String -> boolean
	 * 
	 * @param param
	 * @return
	 */
	public static boolean parseBoolean(String param) {
		if (nullOrBlank(param))
			return false;
		switch (param.charAt(0)) {
		case '1':
		case '3':
		case 'y':
		case 'Y':
		case 't':
		case 'T':
			return true;
		}
		return false;
	}

	/**
	 * 去除空格，如果为null则返回空；如果不为空则返回trim值
	 * 
	 * @param param
	 * @return
	 */
	public static String toEmpty(String param) {
		return toEmpty(param, "");

	}

	public static String toEmpty(String param, String replaceStr) {
		if (param == null)
			return replaceStr;
		else
			return param.trim();

	}

	/**
	 * Check whether null or blank
	 * 
	 * @param param
	 * @return
	 */
	public static boolean nullOrBlank(String param) {
		return (param == null || param.trim().equals("")) ? true : false;
	}

	/**
	 * 依据分界符，将字符串分拆为LIST
	 * 
	 * @param str
	 * @param delim
	 * @return
	 */
	public static List<String> split(String str, String delim) {
		List<String> splitList = null;
		StringTokenizer st = null;

		if (str == null)
			return splitList;

		if (delim != null)
			st = new StringTokenizer(str, delim);
		else
			st = new StringTokenizer(str);

		if (st != null && st.hasMoreTokens()) {
			splitList = new ArrayList<String>();

			while (st.hasMoreTokens())
				splitList.add(st.nextToken());
		}
		return splitList;
	}

	/**
	 * 替换函数
	 * 
	 * @param mainString
	 * @param oldString
	 * @param newString
	 * @return
	 */
	public static String replace(String mainString, String oldString, String newString) {
		if (mainString == null)
			return null;

		if (newString == null) {
			newString = "";
		}

		int i = mainString.lastIndexOf(oldString);
		if (i < 0)
			return mainString;
		StringBuffer mainSb = new StringBuffer(mainString);
		while (i >= 0) {
			mainSb.replace(i, i + oldString.length(), newString);
			i = mainString.lastIndexOf(oldString, i - 1);
		}
		return mainSb.toString();
	}

	/**
	 * 取得随机数
	 * 
	 * @param length
	 *            int 随机数的位数
	 * @return String 返回随机数字符串
	 */
	public static String randomStr(int length) {
		String[] strArray = { "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
				"k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F",
				"G", "H", "I", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
		StringBuffer sb = new StringBuffer("");
		for (int i = 0; i < length; i++) {
			Random r = new Random();
			sb.append(strArray[r.nextInt(55)]);
		}
		return sb.toString();
	}

	/**
	 * 获取当前时间
	 * 
	 * @param pattern
	 * @return
	 */
	public static String getDateTime(String pattern) {
		return getDateTime(pattern, new Date());
	}

	/**
	 * yyyy-MM-dd HH:mm:ss
	 * 
	 * @param pattern
	 * @param date
	 * @return
	 */

	public static String getDateTime(String pattern, Date date) {
		return getDateTime(pattern, date, TimeZone.getDefault());
	}

	/**
	 * 
	 * @param pattern
	 * @param date
	 * @param timeZone
	 * @return
	 */
	public static String getDateTime(String pattern, Date date, TimeZone timeZone) {
		String dt = null;
		if (date == null)
			return dt;

		SimpleDateFormat sdf = new SimpleDateFormat(pattern, java.util.Locale.ENGLISH);
		sdf.setTimeZone(timeZone);
		dt = sdf.format(date);

		return dt;
	}

	/**
	 * 解析时间 , pattern = yyyy-MM-dd HH:mm:ss
	 * 
	 * @param param
	 * @return
	 */
	public static Date parseDate(String param) {
		return parseDate(param, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 解析时间
	 * 
	 * @param param
	 *            字符串内容
	 * @param pattern
	 *            日期格式
	 * @return
	 */
	public static Date parseDate(String param, String pattern) {
		return parseDate(param, pattern, TimeZone.getDefault());
	}

	/**
	 * 解析时间
	 * 
	 * @param param
	 * @param pattern
	 * @param timeZone
	 * @return
	 */
	public static Date parseDate(String param, String pattern, TimeZone timeZone) {
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		sdf.setTimeZone(timeZone);
		try {
			date = sdf.parse(param);
		} catch (Exception ex) {
		}
		return date;
	}

	/**
	 * 保存COOKIE
	 * 
	 * @param response
	 * @param name
	 * @param value
	 * @param saveTime
	 * @param domain
	 */
	public static void saveCookie(HttpServletResponse response, String name, String value, int saveTime, String domain) {
		if (value == null) {
			value = "";
		}
		Cookie cookie = new Cookie(name, value);
		if (domain != null) {
			cookie.setDomain(domain);
		}
		if (saveTime > 0) {
			cookie.setMaxAge(saveTime);
		}
		cookie.setPath("/");
		response.addCookie(cookie);
	}

	/**
	 * 删除对应名称的COOLIE值
	 * 
	 * @param request
	 * @param response
	 * @param cookieName
	 */
	public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
			String domain) {
		// invalidate the cookie
		Cookie cookie = new Cookie(cookieName, null);

		// delete the cookie when the user closes their webbrowser
		cookie.setMaxAge(0);

		if (domain != null)
			cookie.setDomain(domain);
		cookie.setPath("/");

		response.addCookie(cookie);
	}
}
